package com.fsoft.core.common.base.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fsoft.core.Global;
import com.fsoft.core.common.QueryParam;
import com.fsoft.core.common.base.entity.BaseVo;
import com.fsoft.core.common.base.entity.UserVo;
import com.fsoft.core.common.base.mapper.BaseMapper;
import com.fsoft.core.common.base.service.BaseService;
import com.fsoft.core.context.FSContext;
import com.fsoft.core.utils.DateTimeUtils;
import com.fsoft.core.utils.OgnlUtils;

public class BaseServiceImpl<T extends BaseVo> implements BaseService<T> {
	@Autowired
	private BaseMapper<T> mapper;

	@Override
	public T getEntity(String rwid) throws Exception {
		return mapper.selectByKey(rwid);
	}

	@Override
	public List<T> findList(QueryParam query) throws Exception {
		return mapper.selectList(query);
	}

	@Override
	public int save(T entity) throws Exception {
		// ID不允许为空
		if (StringUtils.isBlank(entity.getId()))
			entity.setId(OgnlUtils.randomUpperCaseId());
		// 默认创建时间
		if (OgnlUtils.isEmpty(entity.getCreateTime()))
			entity.setCreateTime(DateTimeUtils.getNowTime());
		// 默认排序
		if (OgnlUtils.isEmpty(entity.getSortNo()))
			entity.setSortNo(BigDecimal.ONE);
		// 默认启用
		if (OgnlUtils.isEmpty(entity.getStatus()))
			entity.setStatus(Global.STATUS_YES);
		if (OgnlUtils.isEmpty(entity.getCreateUserId()))
			entity.setCreateUserId(FSContext.getInstance().getUser().getId());
		return mapper.insert(entity);
	}

	@Override
	public int saveBatch(List<T> list) throws Exception {
		UserVo user = FSContext.getInstance().getUser();
		for (T entity : list) {
			if (StringUtils.isBlank(entity.getId()))
				entity.setId(OgnlUtils.randomUpperCaseId());
			if (OgnlUtils.isEmpty(entity.getCreateTime()))
				entity.setCreateTime(DateTimeUtils.getNowTime());
			if (OgnlUtils.isEmpty(entity.getCreateUserId()))
				entity.setCreateUserId(user.getId());
			// 默认排序
			if (OgnlUtils.isEmpty(entity.getSortNo()))
				entity.setSortNo(BigDecimal.ONE);
			// 默认启用
			if (OgnlUtils.isEmpty(entity.getStatus()))
				entity.setStatus(Global.STATUS_YES);
		}
		return mapper.insertBatch(list);
	}

	@Override
	public int modifyBatch(List<T> list) throws Exception {
		UserVo user = FSContext.getInstance().getUser();
		for (T entity : list) {
			if (OgnlUtils.isEmpty(entity.getModifyTime()))
				entity.setModifyTime(DateTimeUtils.getNowTime());
			if (OgnlUtils.isEmpty(entity.getModifyUserId()))
				entity.setModifyUserId(user.getId());
		}
		return mapper.updateBatch(list);
	}

	@Override
	public int modify(T entity) throws Exception {
		if (OgnlUtils.isEmpty(entity.getModifyTime()))
			entity.setModifyTime(DateTimeUtils.getNowTime());
		if (OgnlUtils.isEmpty(entity.getModifyUserId()))
			entity.setModifyUserId(FSContext.getInstance().getUser().getId());
		return mapper.update(entity);
	}

	@Override
	public int remove(String rwid) throws Exception {
		return mapper.delete(rwid);
	}

	@Override
	public int removeBatch(List<String> ids) throws Exception {
		return mapper.deleteBatch(ids);
	}

}
